package com.dragon.mmochat.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.message.Mmochat_1_10S;
import com.dragon.mmochat.model.enumType.MmochatChenHaoType;
import com.dragon.mmochat.model.enumType.MmochatEquipAttributeType;
import com.dragon.mmochat.model.object.MmochatEquipAttribute;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatDao;
import com.dragon.mmochat.service.MmochatGmService;
import com.dragon.mmochat.service.MmochatMainService;
import com.dragon.mmochat.util.MmochatUtil;

//称号
public class MmochatChenHao implements Serializable {
	private static final long serialVersionUID = -8328491512852713172L;
	private boolean inUse = false; // 是否在使用中
	private MmochatChenHaoType type; // 称谓类别
	private String name; // 称谓显示
	private List<MmochatEquipAttribute> extraAttr = new ArrayList<MmochatEquipAttribute>();// 附加属性
	private String extraEffectInfo = null; // 其它称号效果信息
	private long startTime = System.currentTimeMillis(); // 开始使用时间
	private long lifeTime = MmochatConstant.MS_PER_DAY * 3650; // 有效时间，默认为永久(10年)

	public MmochatChenHaoType getType() {
		return type;
	}

	public void setType(MmochatChenHaoType type) {
		this.type = type;
	}

	public List<MmochatEquipAttribute> getExtraAttr() {
		return extraAttr;
	}

	public void setExtraAttr(List<MmochatEquipAttribute> extraAttr) {
		this.extraAttr = extraAttr;
	}

	public void addAttr(MmochatEquipAttribute attr) {
		if (attr != null) {
			extraAttr.add(attr);
		}
	}

	public int getEquipAttrValue(MmochatEquipAttributeType type) {
		int sum = 0;
		for (MmochatEquipAttribute attr : extraAttr) {
			if (attr.getType() == type) {
				sum += attr.getValue();
			}
		}
		return sum;
	}

	public boolean isInUse() {
		return inUse;
	}

	public void setInUse(boolean inUse) {
		this.inUse = inUse;
	}

	public String getName() {
		if (name == null) {
			return type.toString();
		}
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getExtraEffectInfo() {
		return extraEffectInfo;
	}

	public void setExtraEffectInfo(String extraEffectInfo) {
		this.extraEffectInfo = extraEffectInfo;
	}

	// 删除称号，并保存数据库
	public static boolean delAndUpdateChenHaoAndSaveSql(int roleId,
			MmochatChenHaoType chenhaoType) {
		if (chenhaoType == null) {
			return true;
		}
		MmochatPlayer me = MmochatGmService.getRoleInfoById(roleId);
		if (me == null) {
			return true;
		}
		MmochatChenHao c = me.getChenHaoMap().remove(chenhaoType);
		if (c != null && c.isInUse()
				&& MmochatMainService.isPlayerOnline(roleId)) {
			Mmochat_1_10S pack = new Mmochat_1_10S();
			pack.setMe(me);
			MmochatUtil.sendCommonPack(me, pack);
		}
		if (c != null) {
			try {
				MmochatDao.saveChenhao(me);
			} catch (Exception e) {
				e.printStackTrace();
				me.getChenHaoMap().put(chenhaoType, c);
				return false;
			}
		}
		return true;
	}

	// 增加称号，并保存数据库
	public static boolean addAndUpdateChenHaoAndSaveSql(int roleId,
			MmochatChenHao chenhao) {
		if (chenhao == null) {
			return true;
		}
		MmochatPlayer me = MmochatGmService.getRoleInfoById(roleId);
		if (me == null) {
			return true;
		}
		for (MmochatChenHao c : me.getChenHaoMap().values()) {
			c.setInUse(false);
		}
		chenhao.setInUse(true);
		me.getChenHaoMap().put(chenhao.getType(), chenhao);

		if (MmochatMainService.isPlayerOnline(roleId)) {
			Mmochat_1_10S pack = new Mmochat_1_10S();
			pack.setMe(me);
			MmochatUtil.sendCommonPack(me, pack);
		}
		try {
			MmochatDao.saveChenhao(me);
		} catch (Exception e) {
			e.printStackTrace();
			me.getChenHaoMap().remove(chenhao.getType());
			return false;
		}
		return true;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public long getLifeTime() {
		return lifeTime;
	}

	public void setLifeTime(long lifeTime) {
		this.lifeTime = lifeTime;
	}

	public boolean isTimeOut() {
		return System.currentTimeMillis() - startTime > lifeTime;
	}
}
